//初始化单页view
var viewApi = mui('#app').view({
	defaultPage: '#setting'
});
//初始化单页的区域滚动
mui('.mui-scroll-wrapper').scroll();

var view = viewApi.view;
(function($) {
	//处理view的后退与webview后退
	var oldBack = $.back;
	$.back = function() {
		if(viewApi.canBack()) { //如果view可以后退，则执行view的后退
			viewApi.back();
		} else { //执行webview后退
			oldBack();
		}
	};
	//打开新增页面
	document.getElementById("open").addEventListener('tap', function() {
		$("#name").val("");
		$("#type").val("");
		$("#content").val("");
		title.html("新增");
		add_div.removeClass("mui-hidden");
		edit_div.addClass("mui-hidden");
		mui("#popover").popover("toggle");
	});

	//新增操作
	document.getElementById("add").addEventListener('tap', function() {
		var check = true;
		var ref = sun.createLink("/note");
		mui(".mui-input-group input").each(function() {
			if(!this.value || trim(this.value) == "") {
				var label = this.previousElementSibling;
				mui.alert(label.innerText + "不允许为空");
				check = false;
				return false;
			}
		});
		if(check) {
			var newRef = ref.push({
				"name": $("#name").val(),
				"type": $("#type").val(),
				"content": $("#contact").val(),
				"userId": app.getUser().account
			});
			add_div.addClass("mui-hidden");
			//关闭modal
			mui("#popover").popover("toggle");
			mui.toast("添加成功~");
			//刷新界面
			//初试化缓存
			//加载未完成列表
		}
	});
	//删除准备操作
	var dataId;
	mui("#todolist").on("longtap", "li", function() {
		dataId = this.getAttribute("data-id");
		mui("#operation").popover("toggle");
	});
	//进入编辑列表
	mui(".todo").on("tap", "li", function() {
		var id = this.getAttribute("data-id");
		initParams()
		if(id) {
			$("#name").val($(this).find("#name_").text());
			$("#content").val($(this).find("#content_").text());
			$("#type").val($(this).find("#type_").text());
			$("#name").attr("data-id", id);
			$("#title").html("编辑数据");
			add_div.addClass("mui-hidden");
			edit_div.removeClass("mui-hidden");
			mui("#popover").popover("toggle");
		}
	});
	//更新
	document.getElementById("update").addEventListener("tap", function() {
		var check = true;
		initParams()
		mui(".mui-input-group input").each(function() {
			if(!this.value || trim(this.value) == "") {
				var label = this.previousElementSibling;
				mui.alert(label.innerText + "不允许为空");
				check = false;
				return false;
			}
		});
		if(check) {
			initParams()
			var ref = sun.createLink("/note");
			var newRef = ref.child($("#name").attr("data-id"));
			newRef.update({
				"name": $("#name").val(),
				"content": $("#content").val(),
				"type": $("#type").val()
			});
			//关闭modal
			mui("#popover").popover("toggle");
			edit_div.addClass("mui-hidden")
			mui.toast("修改成功~");
			//初试化缓存
			//加载未完成列表
			//初始化已完成列表
		}
	});
	//删除操作
	$("#del")[0].addEventListener("tap", function() {
		initParams();
		var ref = sun.createLink("/note");
		var newRef = ref.child($("#name").attr("data-id"));
		newRef.remove();
		//关闭modal
		mui("#popover").popover("toggle");
		edit_div.addClass("mui-hidden");
		mui.toast("删除成功~");
		//刷新界面
		//初试化缓存
		//加载未完成列表
		//初始化已完成列表
	});
})(mui);

function parper() {
	//获取数据
	var a ={id:"001",content:"neirong",dateval:"2013-01-01",title:"title"};
	var id = "aa";
	//组装分类
	var notes = [];
	
	var lis;
	var pages;
	for (var o : notes) {
		//拼接li
		var li = "";
		var page = "";
		var data = notes[o];
		li += "<li class='mui-table-view-cell'>";
		li += "<a href='#" + data.id + "' class='mui-navigate-right'>" + data.name + "</a>";
		li += "</li>";
		lis += li;
		
		page +=
	}
	
	var li = "<li class='mui-table-view-cell'>";
	li += "<a href='#" + id + "' class='mui-navigate-right'>" + a.title + "</a>";
	li += "</li>";
	
	var li1 = "<li class='mui-table-view-cell'>";
	li1 += "<a href='#bb' class='mui-navigate-right'>" + a.title + "</a>";
	li1 += "</li>";
	li += li1;
	$(".ulList").html(li);
	//组装具体信息
	var childPage = '<div id="aa" class="mui-page">' +
			'<div class="mui-navbar-inner mui-bar mui-bar-nav">'+
				'<button type="button" class="mui-left mui-action-back mui-btn  mui-btn-link mui-btn-nav mui-pull-left">'+
					'<span class="mui-icon mui-icon-left-nav"></span>分类'+
				'</button>'+
				'<h1 class="mui-center mui-title">新消息通知</h1>'+
			'</div>';
			
		childPage += '<div class="mui-page-content">'
				+'<div class="mui-scroll-wrapper">'
					+'<div class="mui-scroll">'
						+'<ul>'
							+'<li>'
								+'<div class="mui-card">'
									+'<div class="mui-card-header mui-card-media">'
										+'<div class="mui-media-body">'
											+'小M'
											+'<p>发表于 2016-06-30 15:30</p>'
										+'</div>'
									+'</div>'
									+'<div class="mui-card-content">'
										+'<ul>'
											+'<li>sss</li>'
											+'<li>sss</li>'
											+'<li>sss</li>'
											+'<li>sss</li>'
											+'<li>sss</li>'
										+'</ul>'
									+'</div>'
									+'<div class="mui-card-footer"></div>'
								+'</div>'
							+'</li>'
						+'</ul>'
					+'</div>'
				+'</div>'
			+'</div>';
	
	var childPage1 = '<div id="bb" class="mui-page">' +
			'<div class="mui-navbar-inner mui-bar mui-bar-nav">'+
				'<button type="button" class="mui-left mui-action-back mui-btn  mui-btn-link mui-btn-nav mui-pull-left">'+
					'<span class="mui-icon mui-icon-left-nav"></span>设置'+
				'</button>'+
				'<h1 class="mui-center mui-title">新消息通知</h1>'+
			'</div>';
			
		childPage1 += '<div class="mui-page-content">'
				+'<div class="mui-scroll-wrapper">'
					+'<div class="mui-scroll">'
						+'<ul>'
							+'<li>'
								+'<div class="mui-card">'
									+'<div class="mui-card-header mui-card-media">'
										+'<div class="mui-media-body">'
											+'小M1s'
											+'<p>发表于 2016-06-30 15:30</p>'
										+'</div>'
									+'</div>'
									+'<div class="mui-card-content">'
										+'<ul>'
											+'<li>sss</li>'
											+'<li>sss</li>'
											+'<li>sss</li>'
											+'<li>sss</li>'
											+'<li>sss</li>'
										+'</ul>'
									+'</div>'
									+'<div class="mui-card-footer"></div>'
								+'</div>'
							+'</li>'
						+'</ul>'
					+'</div>'
				+'</div>'
			+'</div>';
	$("body").after(childPage).after(childPage1);
}
